package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.constant.BspsTreeNodeConstant;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.GetUserAuthCodeRequest;
import cc.rengu.igas.bsps.facade.response.GetUserAuthCodeResponse;
import cc.rengu.igas.channel.wechat.facade.base.Header;
import cc.rengu.igas.channel.wechat.facade.request.WechatGetAuthCodeRequest;
import cc.rengu.igas.channel.wechat.facade.response.WechatGetAuthCodeResponse;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 获取微信授权码.
 *
 * @author zhangxuran (联调修改)
 * @version 1.0
 * @date 2020.04.07
 * 目前商户服务公众号中并未使用该接口而是通过授权回调的方式获取code，
 * 如果获取code失败前端进行跳转获取code，商户服务中有且仅有微信渠道没有支付宝渠道
 */
public class GetUserAuthCodeService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        GetUserAuthCodeRequest request = new GetUserAuthCodeRequest();
        ConvertUtil.convertOutput(request);
        return request;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        GetUserAuthCodeRequest getUserAuthCodeRequest = (GetUserAuthCodeRequest) request;
        GetUserAuthCodeResponse getUserAuthCodeResponse = new GetUserAuthCodeResponse();
        getUserAuthCodeResponse.setHeader(getUserAuthCodeRequest.getHeader());

        String instId = getUserAuthCodeRequest.getHeader().getInstId();
        String authChannel = getUserAuthCodeRequest.getAuthChannel();
        String redirectUrl = getUserAuthCodeRequest.getRedirectUrl();
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(authChannel) || StringUtil.isEmptyOrNull(redirectUrl)) {
            rglog.error("参数必填性校验失败！instId:<{}>,authChannel:<{}>,redirectUrl:<{}>", instId, authChannel, redirectUrl);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        //区分支付宝与微信
        if (BspsTreeNodeConstant.WECHAT.equals(authChannel.toUpperCase())) {
            Header header = new Header();
            header.setVersion("1.0.0");
            header.setBizType("00");
            header.setTransTime(DateUtil.getCurrentTime());
            header.setTransDate(DateUtil.getCurrentDate());
            header.setChanlId(ChannelEnum.BSPS.getChannelType());
            header.setSrcSysId(AppParamConstant.SYS_ID);
            header.setInstId(instId);
            header.setTxnNum("getauthcode");

            WechatGetAuthCodeRequest wechatGetAuthCodeRequest = new WechatGetAuthCodeRequest();
            wechatGetAuthCodeRequest.setHeader(header);
            wechatGetAuthCodeRequest.setChannelId(ChannelEnum.WXZF.getChannelType());
            wechatGetAuthCodeRequest.setRedirectUrl(getUserAuthCodeRequest.getRedirectUrl());
            wechatGetAuthCodeRequest.setState(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
            DubboService dubboService = new DubboServiceImpl();
            Result<WechatGetAuthCodeResponse> wechatResponse = (Result<WechatGetAuthCodeResponse>) dubboService.callDubboService("wechatPubService", "getAuthCode", wechatGetAuthCodeRequest);

            if (!wechatResponse.isSuccess() || null == wechatResponse.getResult() || !RespCodeEnum.TRANS_SUCCESS.getRespCode().equals(wechatResponse.getResult().getRespCode())) {
                rglog.error("调用微信服务失败,errorCode:<{}>,errorMsg:<{}>", wechatResponse.getResult().getRespCode(), wechatResponse.getResult().getRespDesc());
                throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            }else {
                getUserAuthCodeResponse.setAuthUrl(wechatResponse.getResult().getAuthUrl());
            }
        } else {
            rglog.error("暂不支持获取该渠道授权,机构号:<{}>, AuthChannel:<{}>", instId, authChannel);
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        BizResponse<GetUserAuthCodeResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(getUserAuthCodeResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        GetUserAuthCodeResponse getUserAuthCodeResponse = (GetUserAuthCodeResponse) bizResponse.getResult();
        ConvertUtil.convertInput(getUserAuthCodeResponse);
    }

}
